<?php

/**
 * 图书分类,固定不变的那些分类
 */
class CategoryAR extends CActiveRecord {
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function tableName(){
		return self::tblName();
	}
	
	public static function tblName(){
		return 'ccs8_category';
	}

    public static function querySysAll($condition = ""){
        $sql = "select category_id,name ";
        $sql .= DBUtil::buildFromSingleTbl(self::tblName());
        if($condition != ""){
            $sql .= "where fk_user=0 and $condition";
        }else{
        	$sql .= 'where fk_user=0';
        }
        return DBUtil::queryAll($sql);
    }

    public static function getNameById($cid){
        $sql = 'select name ';
        $sql .= DBUtil::buildFromSingleTbl(self::tblName());
        $sql .= "where category_id = $cid";
        return DBUtil::queryScalar($sql);
    }
    
    public static function queryByUserId($userId = 0){
        if($userId === 0){
            return array();
        }
        $sql = "select category_id,name ";
        $sql .= DBUtil::buildFromSingleTbl(self::tblName());
        $sql .= "where fk_user=$userId";
        return DBUtil::queryAll($sql);
    }
    
    public static function batchGetByCategoryIds($categoryIdArr){
    	if(empty($categoryIdArr)){
    		return array();
    	}
    	$sql = 'select category_id,name ';
    	$sql .= DBUtil::buildFromSingleTbl(self::tblName());
    	$sql .= 'where category_id in ('.join(',',$categoryIdArr).')';
    	return DBUtil::queryAll($sql);
    }

    public static function saveCategory($categoryName,$userId){
        $ar = new CategoryAR;
        $ar->fk_user=$userId;
        $categoryName = mysql_escape_string($categoryName);
        $ar->name=$categoryName;
        $ar->save();
        return $ar->category_id;
    }
}
